﻿<UserControl x:Class="mathview.View.MathView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mathEx="clr-namespace:mathview.Expressions;assembly=mathview"
             xmlns:viewEx="clr-namespace:mathview.View.Expressions"
             xmlns:math="clr-namespace:mathview.View"
             FontFamily="Cambria Math">
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="Bool2Vis" />
        
        <!--Atom layout.-->
        <DataTemplate DataType="{x:Type mathEx:Atom}">
            <TextBlock Text="{Binding Path=Text}" VerticalAlignment="Center" />
        </DataTemplate>
        <!--Variable layout.-->
        <DataTemplate DataType="{x:Type mathEx:Variable}">
            <TextBlock VerticalAlignment="Center">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Text" Value="{Binding Path=Text}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=ItalicLetters, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type math:MathView}}}" Value="True">
                                <Setter Property="Text" Value="{Binding Path=ItalicText}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </DataTemplate>
        <!--Operator layout.-->
        <DataTemplate DataType="{x:Type mathEx:Operator}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="&#x2006;" VerticalAlignment="Center" Visibility="{Binding Path=HasLeftWhitespace, Converter={StaticResource Bool2Vis}}" />
                <TextBlock Text="{Binding Path=Text}" VerticalAlignment="Center" />
                <TextBlock Text="&#x2006;" VerticalAlignment="Center" Visibility="{Binding Path=HasRightWhitespace, Converter={StaticResource Bool2Vis}}" />
            </StackPanel>
        </DataTemplate>
        <!--Fraction layout.-->
        <DataTemplate DataType="{x:Type mathEx:Fraction}">
            <viewEx:FractionView Fraction="{Binding}" />
        </DataTemplate>
        <!--Sub/Superscript layout.-->
        <DataTemplate DataType="{x:Type mathEx:SubAndSuperscript}">
            <viewEx:SubAndSuperscriptView SubAndSuperscript="{Binding}" />
        </DataTemplate>
        <!--Whitespace layout.-->
        <DataTemplate DataType="{x:Type mathEx:Whitespace}">
            <viewEx:WhitespaceView Whitespace="{Binding}" />
        </DataTemplate>
        <!--Block layout.-->
        <DataTemplate DataType="{x:Type mathEx:Block}">
            <ItemsControl ItemsSource="{Binding Path=Expressions}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <viewEx:BlockView IsVertical="{Binding Path=IsVertical}" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DataTemplate>
    </UserControl.Resources>
    
    <!--Here comes the content.-->
    <ContentPresenter Content="{Binding Path=Source, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
</UserControl>